#ifndef FUNCBLOCKBASE_H
#define FUNCBLOCKBASE_H

#include <QWidget>

#include "schematicbase.h"
#include "wirebase.h"
#include "textbase.h"
#include "pinbase.h"

enum configType_t
{
	ctUnknown,
	ctConst,
	ctNetPin,
	ctParam,
	ctChecksum
};

class ConfigBase
{
public:
	enum configType_t type;
	unsigned int value;
};

enum paramDataType_t
{
	pdtSByte,
	pdtUByte,
	pdtSInt,
	pdtUInt,
	pdtSLong,
        pdtULong,
        pdtCaption
};

class ParamBase
{
public:
	enum paramDataType_t datatype;
	QString name;
	int min;
	int max;
	unsigned int paramid;
        QString value;

};

enum pinDirection_t
{
    dirUnknown,
    dirInput,
    dirOutput
};

class PinFB : public PinBase
{
public:
        PinFB(QObject *parent, int x, int y, unsigned int id, unsigned char datawidth, int offset_x, int offset_y, QString name, unsigned int pinno, enum pinDirection_t dir);
	QPoint offsetFB;
	unsigned int getPinNo() {return pinno;};
        enum pinDirection_t getDirection() {return dir;};

private:
	unsigned int pinno;
        enum pinDirection_t dir;
};

class PinBase;
class SchematicBase;
class TextBase;

class FuncBlockBase : public QWidget
{
	Q_OBJECT
public:
	explicit FuncBlockBase(SchematicBase *parent);
	~FuncBlockBase();
	bool addWireToPin(int x, int y, WireBase *wire);
	bool addPinToPin(int x, int y, PinBase *pin);
	unsigned int getPinCount();
	PinFB* getPinRef(unsigned int idx);
	unsigned int getParamCount();
	ParamBase* getParamRef(unsigned int idx);
	PinBase* findPinById(unsigned int pinid);
	bool setPinIdByNo(unsigned int no, unsigned int id);
        void resetMemoryFlagIdx();
        PinFB* getPinRefByNo(unsigned int no);
        QString getCaption();
        void setParamValueById(unsigned int id, QString value);
        ParamBase* getParamById(unsigned int id);
        unsigned int getConfigCount();
        ConfigBase* getConfigRef(unsigned int idx);

signals:

public slots:
        void onSelectChanged(FuncBlockBase *fb);

private:
	void paintEvent(QPaintEvent *);
	PinBase* findPin(int x, int y, unsigned char datawidth);
	QPoint offset;
	bool bMoveMode;

protected:
	QList<QLine*> *vimage; // Linienbeschreiung des grafischen Symbols
	QList<PinFB*> *vpins; // Koordinaten der Anschlusspins
	QList<TextBase*> *texts;
	QString name;
	QList<ConfigBase*> *configList;
	QList<ParamBase*> *paramList;
	SchematicBase *schematicParent;
	virtual void mousePressEvent(QMouseEvent* me);
	virtual void mouseMoveEvent(QMouseEvent* me);
	virtual void mouseReleaseEvent(QMouseEvent* me);
	virtual void moveEvent(QMoveEvent *event);
};

#endif // FUNCBLOCKBASE_H
